var net = require("../../utils/net.js");
var app = getApp();
var utils = require("../../utils/util.js");
const Charts = require("../common/wxcharts.js");
var app = getApp();
const date = new Date()
const years = []
const months = []
const days = []

for (let i = 1990; i <= date.getFullYear(); i++) {
  years.push(i)
}

for (let i = 1; i <= 12; i++) {
  months.push(i)
}
Page({
  data: {
    isDown: false,
    percent: 0,
    shop_month_captical: "",
    shop_card_num: "",
    shop_course_num: "",
    shop_course_use: "",
    years: years,
    year: date.getFullYear(),
    months: months,
    month: 2,
    days: days,
    day: 2,
    array:[],
    value: [9999, 1, 1],
    index: 0,
    date: '2018-03',
    selectPerson: true,
    selectArea: false,
    shop_id: wx.getStorageSync("name")["shop_id"],
    mobile: wx.getStorageSync("name")["mobile"],
    boss_id: wx.getStorageSync("name")["boss_id"]
  
  },
  bindChange: function (e) {
    const val = e.detail.value
    this.setData({
      year: this.data.years[val[0]],
      month: this.data.months[val[1]],
     
     
    })
  },

  bindDateChange: function (e) {
    this.setData({
      date: e.detail.value
    })
  },
  bindTimeChange: function (e) {
    this.setData({
      time: e.detail.value
    })
  },
  clickPerson: function () {
    var selectPerson = this.data.selectPerson;
    if (selectPerson == true) {
      this.setData({
        selectArea: true,
        selectPerson: false,
      })
    } else {
      this.setData({
        selectArea: false,
        selectPerson: true,
      })
    }
  },
  bindPickerChange:function(e) {
    this.setData({
      index: e.detail.value
      
    });
    wx.setStorageSync("sss", this.data.array[e.detail.value].id)

    
    this.anniu();
  }, 

  search: function () {
    this.anniu();
    //进度条
    this.setData({
      isDown: true,
      percent: 100,
    })
  },
  anniu: function () {
    var that = this;
    net.request({
      url: "/MonthStatistic",
      data: {
        shop_id: that.data.array[that.data.index].id,
        boss_id: wx.getStorageSync("name")["boss_id"],
        month: that.data.date,
    
    
      },
      
      success: function (res) {
        if (res.data.flag) {
          that.setData({
            shop_month_captical: res.data.data.shop_month_captical || 0,
            shop_card_num: res.data.data.shop_card_num || 0,
            shop_course_num: res.data.data.shop_course_num || 0,
            shop_course_use: res.data.data.shop_course_use || 0,
            list: res.data.data.list
          })
          that.zhuzhang();
        }
        console.log(res);
      },
    })
  },
  zhuzhang: function () {
    var categoriesList = [];
    var seriesList = [];
    var dt = this.data.list;
    var len = this.data.list.length - 1;
    for (var i = len>5?5:len; i >=0; --i) {
      categoriesList.push("" + dt[i].key.substring(5));
      seriesList.push("" + dt[i].value);
    }
   
    console.log(this.data.list)
    new Charts({
      canvasId: 'columnCanvas',
      type: 'column',
      categories: categoriesList,
      
      series:[{

        name: '入场人数统计',

        data: seriesList
    
      }],
     
      itemStyle: {
        //通常情况下：


        normal: {
          //每个柱子的颜色即为colorList数组里的每一项，如果柱子数目多于colorList的长度，则柱子颜色循环使用该数组
          barBorderRadius: [10, 10, 10, 10,10],
          color: function (params) {
            var colorList = ['rgb(137,208,176)', 'rgb(137,208,176)', 'rgb(25,46,94)', 'rgb(195,229,235)'];
            return colorList[params.dataIndex];
          }
        },
         normal: {
          //柱形图圆角，初始化效果
          barBorderRadius: [10, 10, 10, 10,10]
        }
      },
      yAxis: {
        min: 0,
        max:500,
        format: function (val) { 
           return val;

        }
      

      },xAxis:{
        disableGrid:true
      },
      width: 300,
      height: 200,
      dataLabel: true
    });
  },
  
  onLoad: function (options) {
    var that = this;
    var month = new Date().getMonth() + 1;

    if (month < 10) {
      month = "0" + month
    }
    var date = new Date().getFullYear() + "-" + month;
    var time = utils.formatTime(new Date());
    this.setData({
      date: date
    });
    wx.showLoading({ duration: 1000 })
    net.request({
      url: '/GetShopList',
      data: {
        mobile: wx.getStorageSync("name")["mobile"],
        
      },

      success: function (res) {
        if (res.data.flag) {
        wx.setStorageSync("sss", res.data.data[0].id)
          that.setData({
            array: res.data.data,

          })
          that.anniu();
        }

      }
    })

  },
  
  onPullDownRefresh: function () {//下拉刷新
    // wx.startPullDownRefresh()
    var that = this;
    net.request({
      url: "/MonthStatistic",
      data: {
        shop_id: that.data.array[that.data.index].id,
        month: that.data.date,
        boss_id: wx.getStorageSync("name")["boss_id"],

      },

      success: function (res) {
       
        if (res.data.flag) {
          boss_id: wx.setStorageSync("boss",that.data.boss_id)
          that.setData({
            shop_month_captical: res.data.data.shop_month_captical || 0,
            shop_card_num: res.data.data.shop_card_num || 0,
            shop_course_num: res.data.data.shop_course_num || 0,
            shop_course_use: res.data.data.shop_course_use || 0,
            list: res.data.data.list
          })
          that.zhuzhang();
        }
        console.log(res);
      },
      complete: function () {
        wx.hideNavigationBarLoading() //完成停止加载
        wx.stopPullDownRefresh() //停止下拉刷新

      }  
    })
  },

})
